<?php
Class Controller_Manage_Information extends Controller_Abstract
{
	function actionCreate()
	{
		$this->_view['title'] = '添加素材';
	}
	
	function actionCreateSave()
	{
		$this->_view['title'] = '添加素材保存';
		
		//dump($_POST);exit;
		if($_FILES['file-img']['size']>0&&$_FILES['file-caseimg']['size']>0&&$_FILES['file-interactionimg']['size']>0)
		{
			$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/info/img');//缩略图
			$caseimg = Helper_BSS_Normal::upload($_FILES['file-caseimg'], Q::ini('custom_system/base_url').'upload/info/caseimg');//大图
			$interactionimg = Helper_BSS_Normal::upload($_FILES['file-interactionimg'], Q::ini('custom_system/base_url').'upload/info/interactionimg');//大图
			if ($img['ack'] == SUCCESS && $caseimg['ack'] == SUCCESS && $interactionimg['ack'] == SUCCESS)
			{
				$caseimgsrc = $caseimg['data'];
				$imgsrc = $img['data'];
				$interactionimgsrc = $interactionimg['data'];
			}
			else
			{
				return $this->_redirectMessage('提交失败', "图片上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		
		$title = isset($_POST['title'])?$_POST['title']:'';
		$author = isset($_POST['author'])?$_POST['author']:'';
		$tag = isset($_POST['tag'])?$_POST['tag']:'';
		$category = isset($_POST['category'])?$_POST['category']:'';
		$status = isset($_POST['status'])?$_POST['status']:'';
		$content = isset($_POST['content'])?$_POST['content']:'';
		$desc = isset($_POST['desc'])?$_POST['desc']:'';
		$channel = isset($_POST['channel'])?$_POST['channel']:'';
		$time = date('Y-m-d H:i:s');
		
		if((!isset($caseimgsrc)||!$caseimgsrc)||(!isset($imgsrc)||!$imgsrc)||(!isset($interactionimgsrc)||!$interactionimgsrc))
		{
			return $this->_redirectMessage('提交失败', "图片上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
		}
		
		if($title && $content)
		{	
			$new = new Information();
			$new->title = $title;
			$new->tag = $tag;
			$new->category = $category;
			$new->content = $content;
			$new->status = $status;
			$new->author = $author;
			$new->time = $time;
			$new->img = $imgsrc;
			$new->caseimg = $caseimgsrc;
			$new->interactionimg = $interactionimgsrc;
			$new->desc = $desc;
			$new->channel = $channel;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Information/Create'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "标题和内容不能为空", url('Manage::Information/Create'),FAILURE);
		}
	}
	
	function actionList()
	{
		$this->_view['title'] = '素材列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = Information::search($url_arr,$cp, $ps);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Information/List', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['information'] = $result['data'];
		}
	}

    function actionShow()
    {
        $this->_view['title'] = '显示素材详情';
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $code = Information::find('id=?', $id)->asArray()->getOne();
        if ($code)
        {
            $this->_view['code'] =  $code['code'];
        }
        else
        {
            return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Information/List'), FAILURE);
        }
    }
	
	function actionDetail()
	{
		$this->_view['title'] = '素材详细';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$info = Information::find('id=?', $id)->asArray()->getOne();
		if ($info)
		{
			$this->_view['info'] =  $info;
		}
		else
		{
			return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Information/List'), FAILURE);
		}
	}
	
	function actionEdit()
	{
		$this->_view['title'] = '素材编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$info = Information::find('id=?', $id)->asArray()->getOne();
		if ($info)
		{
			$this->_view['info'] = $info;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '没有该产品存在', url('Manage::Information/List'), FAILURE);
		}
	}
	
	function actionEditSave()
	{
		$this->_view['title'] = '素材编辑保存';
		if (isset($_POST)&&$_POST)
		{
			//dump($_POST);exit;
			//上传图片
			if(isset($_FILES['file-img']['size'])&&$_FILES['file-img']['size']>0)
			{
				$img = Helper_BSS_Normal::upload($_FILES['file-img'], Q::ini('custom_system/base_url').'upload/info/img');//缩略图
				if ($img['ack'] == SUCCESS)
				{
					$imgsrc = $img['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "封面图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			if(isset($_FILES['file-caseimg']['size'])&&$_FILES['file-caseimg']['size']>0)
			{
				$caseimg = Helper_BSS_Normal::upload($_FILES['file-caseimg'], Q::ini('custom_system/base_url').'upload/info/caseimg');//大图
				if ($caseimg['ack'] == SUCCESS)
				{
					$caseimgsrc = $caseimg['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "案例图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
			}
			
			
			if(isset($_FILES['file-interactionimg']['size'])&&$_FILES['file-interactionimg']['size']>0)				
			{
				$interactionimg = Helper_BSS_Normal::upload($_FILES['file-interactionimg'], Q::ini('custom_system/base_url').'upload/info/interactionimg');//大图
				if ($interactionimg['ack'] == SUCCESS)
				{
					$interactionimgsrc = $interactionimg['data'];
				}
				else
				{
					return $this->_redirectMessage('提交失败', "互动图上传失败，请重新选择图片", $_SERVER['HTTP_REFERER'],FAILURE);
				}
				
			}
			
			$id = isset($_POST['id'])?$_POST['id']:'';
			$title = isset($_POST['title'])?$_POST['title']:'';
			$author = isset($_POST['author'])?$_POST['author']:'';
			$tag = isset($_POST['tag'])?$_POST['tag']:'';
			$category = isset($_POST['category'])?$_POST['category']:'';
			$status = isset($_POST['status'])?$_POST['status']:'';
			$content = isset($_POST['content'])?$_POST['content']:'';
			$desc = isset($_POST['desc'])?$_POST['desc']:'';
			$channel = isset($_POST['channel'])?$_POST['channel']:'';
			$time = date('Y-m-d H:i:s');
			$imgsrc = isset($imgsrc)?$imgsrc:'';
			$caseimgsrc = isset($caseimgsrc)?$caseimgsrc:'';
			$interactionimgsrc = isset($interactionimgsrc)?$interactionimgsrc:'';
			
			if($title && $content)
			{	
				$edit = Information::find('id=?',$id)->getOne();
				
				if(!isset($edit['id']) || !$edit['id'])
				{
					return $this->_redirectMessage('提交失败', "文章不存在", url('Manage::Information/Edit'),FAILURE);
				}
				
				$edit->title = $title;
				$edit->tag = $tag;
				$edit->category = $category;
				$edit->content = $content;
				$edit->status = $status;
				$edit->author = $author;
				$edit->time = $time;
				
				if($imgsrc)
				{
					$edit->img = $imgsrc;
				}
				
				if($caseimgsrc)
				{
					$edit->caseimg = $caseimgsrc;
				}
				
				if($interactionimgsrc)
				{
					$edit->interactionimg = $interactionimgsrc;
				}
				
				$edit->desc = $desc;
				$edit->channel = $channel;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('提交失败', "标题和内容不能为空", url('Manage::Information/Edit'),FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Information/List'), FAILURE);
		}
	}
	
	function actionDelete()
	{
		$this->_view['title'] = '素材删除';
		$result = Information::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}

    function actionStop()
    {
        $this->_view['title'] = '停用';
        //echo  $_GET['id'];exit;
        $result = Code::find(array('id'=>$_GET['id']))->getOne();
        if ($result)
        {
            $result->status = 2;
            $result->save();
            echo 1;exit;
        }
        else
        {
            echo 0;exit;
        }
    }
}